package com.atguigu.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author lzc
 * @Date 2022/7/24 15:14
 */
public class IkUtil {
    public static List<String> split(String kw) {
        ArrayList<String> words = new ArrayList<>();
        // String  ->   Reader
        StringReader reader = new StringReader(kw);
        IKSegmenter seg = new IKSegmenter(reader, true);
        try {
            Lexeme next = seg.next();// 获取第一个分词
            while (next != null) {
                String word = next.getLexemeText();  // 获取文本数据
                
                words.add(word);
                
                next = seg.next();
        
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        Set<String> set = new HashSet<>(words);
        words.clear();
        words.addAll(set);
    
        return words;
    }
    
    public static void main(String[] args) {
        System.out.println(split("手机 华为 智能 手机"));
    }
}
